


// 插屏广告
var interstitialAd = null;
let interstitial = {
	load(id) {
		if (uni.createInterstitialAd) {
			interstitialAd = uni.createInterstitialAd({
				adUnitId: id
			})
			interstitialAd.onLoad(() => {
				console.log('插屏 广告加载成功')
			})
			interstitialAd.onError((err) => {
				console.log('插屏 广告加载失败', err)
			})
			interstitialAd.onClose((res) => {
				console.log('插屏 广告关闭', res)
			})
		}
	},
	show() {
		if (interstitialAd) {
			interstitialAd.show()
			.catch((err) => {
				console.error(err)
			})
		}
	}
}
let videoAd = null;

let rewarded = {
	load(id, e) {
	    if (uni.createRewardedVideoAd) {
	      videoAd = uni.createRewardedVideoAd({
	        adUnitId: id
	      })
	      videoAd.onError(err => { console.log('激励视频 广告显示失败')})
	      videoAd.onClose((status) => {
	        if (status && status.isEnded || status === undefined) {
	          e()
	        } else {}
	      })
	    }
	  },
	   show() {
	      if (videoAd) {
	        videoAd.show().catch(() => {
	          // 失败重试
	          videoAd.load()
	            .then(() => videoAd.show())
	            .catch(err => {
	              console.log('激励视频 广告显示失败')
	            })
	        })
	      }
	    }
}

module.exports = {
	interstitial,
	rewarded
};